let name = $.cookie("keyword");

$(function () {
  var search = function () {
    //获取分页数据

    var goodlist = [];
    var page = 1;
    var pagesize = 10;

    function getGoodList(name) {
      // const obj={

      // }
      // obj.keyword=name
      $.ajax({
        url: "../server/fenye.php",
        async: false,
        type: "post",
        data: {
          page, //当前页码
          pagesize, //显示条数
          keyword: name, //搜索关键词
        },
      }).then((res) => {
        console.log(res);
        data = JSON.parse(res);
        console.log(data);
        goodlist = data;
        // var data=res[data]
        // console.log(data)
        // goodlist=JSON.parse(data)
        console.log(goodlist);

        var str = "";
        //商品列表数据渲染
        $.each(goodlist.data, function (index, item) {
          str += `
         <li class="goods_item j_goods_item">
         <div class="goods_body">
             <a href="../page/cart.html?id=${item.id}" class="goods_pic">
                 <img src="${item.img_big}" alt="">
             </a>
             <a href="javascript:;" class="goods_name ellipsis">${item.good_name}</a>
             <div class="goods_thumbs j_goods_thumbs">
                 <ul class="thunbs_list j_thumbs_list">
                     <li>
                         <a href="">
                             <img src="${item.img_middle}" alt="">
                         </a>
                     </li>
                 </ul>
             </div>
             <a href="javascript:;" class="goods_price">
                 <span>￥${item.price}</span>
             </a>
         </div>
     </li>
         
         
         `;
        });

        //获取分页数目转化成数组
        var pages = goodlist.pages;
        var arryPage = new Array(pages);
        console.log(arryPage);
        $(".good_list").html(str);
        console.log(arryPage.length); //2
        if (arryPage.length == 0) {
          $(".search_empty").show();
        }
        //底部分页渲染
        // let pageplage = "";
        $(".page_select .page_select").html(`${goodlist.page}`);
        $(".page_select span").html(`${goodlist.pages}`);
        //分页页数导航渲染
        let template = "";
        $.each(arryPage, function (index, item) {
          template += `<li>
           <a href="javascript:;" >${index + 1}</a>
         </li>`;
        });
        //分页总体导入
        // $(".list_bottom").html(pageplage);

        //导航导入
        $(".page_select_list").html(template);

        //所有商品和匹配数目结果数据展示
        console.log(goodlist.count);
        $(".cate-title span").html(`(${goodlist.count})`);
        $(".list_top_fr .result").html(`匹配结果(${goodlist.count})`);
      });
    }

    getGoodList(name);

    //下一页商品数据
    $(".next").click(function () {
      if (page < goodlist.pages) {
        page++;
        getGoodList();
      } else {
        //tips弹窗
        toast("没有更多商品了");
      }
    });

    //上一页商品数据
    $(".prev").click(function () {
      if (page == 1) {
        toast("已经是第一页了");
        return;
      }
      if (page <= goodlist.page) {
        page--;
        getGoodList();
      } else {
        toast("已经是第一页了");
      }
    });

    //小分页导航显式隐藏与背景变色
    $(".page_select>.page_select").click(function () {
      $(".page_select_list").toggle();
    });
    $(".page_select_list").on("mouseenter", "li", function () {
      $(this).addClass("active2").siblings().removeClass("active2");
    });

    //点击小导航发送请求
    $(".page_select_list").on("click", "li", function () {
      var middlePage = Number($(this).text());
      page = middlePage;
      getGoodList();
    });

    //搜索指定商品
    $(".header .nav .nav_search button").click(function () {
      // console.log(1)
      let keyword = $(".naval_search").val();
      getGoodList(keyword);
    });
  };

  window.search = search;
});

$(function () {
  var selfSearch = function () {
    //搜索指定商品
    $(".header .nav .nav_search button").click(function () {
      let keyword = $(".naval_search").val();
      $.cookie("keyword", keyword, { expires: 7 });
      window.location.href = "../page/shop.html";
    });
  };
  window.selfSearch = selfSearch;
});
